package ai.george.quickstart.controller;


import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "chatClient错误构建示例")
@RestController
@RequestMapping("/error/example")
public class ChatClientErrorBuildExampleController {

    private final ChatClient simpleChatClient;


    private final ChatClient memoryChatClient;


    /**
     * 使用构造器注入ChatClient
     *
     * @param chatClientBuilder 由starter自动创建
     */
    public ChatClientErrorBuildExampleController(ChatClient.Builder chatClientBuilder) {
        //公共的chatOptions
        DashScopeChatOptions chatOptions = DashScopeChatOptions.builder()
                .withModel(DashScopeApi.ChatModel.QWEN_PLUS.getValue())
                .withTopP(0.7)
                .build();


        /**
         * 1. 创建一个 ChatClient（没有配置默认的Advisor）
         */
        this.simpleChatClient = chatClientBuilder
                // 设置 ChatClient 中 ChatModel 的 Options 参数
                .defaultOptions(chatOptions)
                .build();


        /**
         * 2. 创建一个 ChatClient（配置了默认的Advisor,直接支持会话记忆功能,不过要使用这个chatClient，需要设置会话id）
         */

        //公共的chatOptions
        DashScopeChatOptions chatOptions2 = DashScopeChatOptions.builder()
                .withModel(DashScopeApi.ChatModel.QWEN_PLUS.getValue())
                .withTopP(0.7)
                .build();
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
//                .chatMemoryRepository() // 可以指定具体的存储仓库
//                .maxMessages(10) // 设置窗口大小（最大记忆的对话消息数量）
                .build();

        MessageChatMemoryAdvisor chatMemoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory).build();
        this.memoryChatClient = chatClientBuilder
                .defaultAdvisors(chatMemoryAdvisor)
                .defaultOptions(chatOptions2)
                .build();

        System.out.println("=========");
    }


}
